﻿// Copyright (C) 2014 Can Batu Erdem
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System.Collections.Specialized;
using System.Text;
using System.Web;

namespace Cyrene.Strategy
{
    public class HtmlTableGeneratorStrategy : TableGeneratorStrategy
    {
        internal override string Generate(string header, NameValueCollection nameValueCollection)
        {
            return GenerateTableHeader(header) + GenerateTableContent(nameValueCollection) + GenerateTableFooter();
        }

        internal override string GenerateTableHeader(string header)
        {
            return string.Format("<table style=\"width:100%\" cellpadding=\"1\" cellspacing=\"0\"><tr style=\"background-color:black;color:white;\"><td colspan=\"2\"><h3>{0}</h3></td></tr>", header);
        }

        internal override string GenerateInlineHeader(string header)
        {
            return string.Format("<tr style=\"background-color:black;color:white;\"><td colspan=\"2\"><b>{0}</b></td></tr>", header);
        }

        internal override string GenerateTableContent(NameValueCollection nameValueCollection)
        {
            StringBuilder sb = new StringBuilder();
            if (nameValueCollection.Count > 0)
            {
                sb.AppendFormat("<tr><td style=\"width:20%\"><b>{0}</b></td><td>{1}</td></tr>", nameValueCollection.Keys[0], HttpUtility.HtmlEncode(nameValueCollection[0]));
                for (int i = 1; i < nameValueCollection.Count; i++)
                {
                    string alternateBackground = ((i % 2) == 0) ? string.Empty : " style=\"background-color:silver;\"";
                    sb.AppendFormat("<tr{0}><td><b>{1}</b></td><td>{2}</td></tr>", alternateBackground, nameValueCollection.Keys[i], HttpUtility.HtmlEncode(nameValueCollection[i]));
                }
            }
            return sb.ToString();
        }

        internal override string GenerateTableContentSingleColumn(string[] lines)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < lines.Length; i++)
            {
                string alternateBackground = ((i % 2) == 0) ? string.Empty : " style=\"background-color:silver;\"";
                sb.AppendFormat("<tr{0}><td colspan=\"2\">{1}</td></tr>", alternateBackground, HttpUtility.HtmlEncode(lines[i]));
            }
            return sb.ToString();
        }

        internal override string GenerateTableFooter()
        {
            return "</table><br>";
        }
    }
}
